<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 
 */
namespace osc\member\controller;
use osc\common\controller\AdminBase;
use think\Db;
class RefundBackend extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','会员');
		$this->assign('breadcrumb2','退换申请');		
	}
	
	function index(){
		
		$param=input('param.');
		
		$query=array();
		
		if(isset($param['username'])){		
			$map['username']=array('like',"%".$param['username']."%");
			$query['username']=urlencode($param['username']);
		}
		
		if(isset($param['order_sn'])){		
			$map['order_sn']=array('eq',$param['order_sn']);
			$query['order_sn']=urlencode($param['order_sn']);
		}
		
		if(isset($param['status'])){		
			$map['status']=array('eq',$param['status']);
			$query['status']=urlencode($param['status']);
		}
		
		$map['refund_id']=array('gt',0);
		
		$this->assign('list',Db::name('refund')->where($map)->order('refund_id desc')->paginate(config('page_num'),false,$query));

		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
		
		return $this->fetch();
	}
	
	function refund(){
		
		$refund=Db::name('refund')->find(input('param.id'));//dump($refund);die;
		
		$list=osc_order()->refund_goods_list($refund['order_id'],$refund['uid']);		
		
		$this->assign('refund',$refund);
		
		$this->assign('list',$list);
		
		$this->assign('crumbs','订单退换');
		
		return $this->fetch();
		
	}
	
	function deal_refund(){
		if(request()->isPost()){	
			
			$data=input('param.');
			
			$total=Db::name('order_total')->where(array('order_id'=>$data['order_id'],'code'=>'sub_total'))->find();
			//退款金额
			if($data['amount']>$total['value']){
				return array('error'=>'退款金额错误，最高可退款金额'.$total['value']);
			}
			
			$refund=Db::name('refund')->find($data['refund_id']);
			
			//同意
			if($data['status']==2){
				//退款
				Db::name('member')->where('uid',$refund['uid'])->setInc('predeposit',$data['amount']);	
				//退款到预存款中的记录
				Db::name('predeposit')->insert(array('username'=>$refund['username'], 'uid'=>$refund['uid'], 'money'=>$data['amount'], 'description'=>'订单退款到预存款', 'prefix'=>1, 'creat_time'=>time(), 'pay_status'=>1, 'admin_id'=>UID ));
				
				Db::name('refund')->where('refund_id',$data['refund_id'])->update(array('step'=>3,'status'=>2,'admin_name'=>session('user_auth.username'),'admin_message'=>'同意退款，退款金额'.$data['amount']));
				
				Db::name('order')->where('order_id',$data['order_id'])->update(array('order_status_id'=>config('close_order_status_id')));
				
				Db::name('order_history')->insert(array('order_status_id'=>config('close_order_status_id'),'order_id'=>$data['order_id'],'comment'=>'同意退款申请','date_added'=>time()));
				
				storage_user_action(UID,session('user_auth.username'),config('BACKEND_USER'),'同意退款申请');
			}
			//不同意
			if($data['status']==3){
				
				if(empty($data['admin_message'])){
					return array('error'=>'请填写不同意申请的原因');
				}
				
				Db::name('refund')->where('refund_id',$data['refund_id'])->update(array('step'=>3,'status'=>3,'admin_name'=>session('user_auth.username'),'admin_message'=>'不同意退款，原因：'.$data['admin_message']));
				
				Db::name('order')->where('order_id',$data['order_id'])->update(array('order_status_id'=>$refund['order_status_id']));
				
				Db::name('order_history')->insert(array('order_status_id'=>$refund['order_status_id'],'order_id'=>$data['order_id'],'comment'=>'不同意退款申请','date_added'=>time()));
				
				storage_user_action(UID,session('user_auth.username'),config('BACKEND_USER'),'不同意退款申请');
			}
			
			return array('success'=>'处理完成','url'=>url('RefundBackend/index'));
			
		}
	}
	
	function del(){
		if(request()->isGet()){	
			$r= $this->single_table_delete('Refund','删除了退换货申请');
			if($r){
				$this->redirect('RefundBackend/index');
			}
		}
	}
}
